package shopping;

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int shopcar=100;
        System.out.println("商品如下：");
        System.out.println("雪碧,可乐,果汁,苹果,香蕉");
        goods[] goodAllInformation = new goods[5];
        goodAllInformation[0] = new goods("雪碧", 3, 100, new String[]{"有糖","无糖"});
        goodAllInformation[1] = new goods("可乐", 3, 100, new String[]{"有糖","无糖"});
        goodAllInformation[2] = new goods("果汁", 5, 50, new String[]{"苹果","梨"});
        goodAllInformation[3] = new goods("苹果", 5, 30, new String[]{"红","青"});
        goodAllInformation[4] = new goods("香蕉", 7, 30, new String[]{"大","小"});
        Scanner sc = new Scanner(System.in);
        goods[] goodInput = new goods[30];//goodInput表示用户输入的商品信息

        int exd=0;
        String name;
        System.out.println("结算或退出，请输入q");
        System.out.println("要删除购物车中的商品，请输入d");
        System.out.println("要查看购物车中的商品，请输入l");
        while(true) {
            goodInput[exd]=new goods();
            System.out.printf("请输入第%d个加入购物车的商品名：",exd+1);
            System.out.println();
            name = sc.nextLine();
            if(name.equals("q")){
                //输出账单goodInput
                //无商品结算，输出0.00
                if(exd==0){
                    System.out.println("退出");
                }else{
                    //结算
                    int sum=0;
                    for (int i = 0; i < exd; i++) {
                        sum+=goodInput[i].getCount();
                    }
                    System.out.println("总额为："+sum);
                }
                break;
            } else if (name.equals("l")) {
                //查看购物车中的商品
                for (int i = 0; i < exd; i++) {
                    System.out.println("商品名："+goodInput[i].getName()+" 种类："+goodInput[i].getKinds()+" 数量"+goodInput[i].getCount());
                }
            } else if(name.equals("d")){
                while (true){
                    if(exd==0){
                        System.out.println("请将商品放入购物车后再选择删除！");
                    }else{
                        //删除购物车中的商品
                        System.out.println("输入你要删除的商品名(按qd退出):");
                        String delet=sc.nextLine();
                        if(delet.equals("qd")){
                            break;
                        }
                        int flag=0;
                        int mark=0;
                        for (int i = 0; i < exd; i++) {
                            if(delet.equals(goodInput[i].getName())){
                                flag=1;
                                mark=i;
                            }
                        }
                        if(flag==0){
                            System.out.println("您未购买此商品，请输入你已购的商品：");
                        }else{
                            if(exd>1){
                                int exs=exd-1;
                                goodInput[mark]=goodInput[exs];
                            }
                        }
                        exd--;
                    }
                }
            }else{
                int flag=0;
                int mark=0;
                for (int i = 0; i < goodAllInformation.length; i++) {
                    if(name.equals(goodAllInformation[i].getName())){
                        flag=1;
                        mark=i;
                        System.out.println(goodAllInformation[mark].getName()+"的种类有"+goodAllInformation[mark].getKinds()+
                                "数量有"+goodAllInformation[mark].getCount()+"个（斤），价格为"+goodAllInformation[mark].getPrice()+"元");
                    }
                }
                if(flag==0){
                    System.out.println("无此商品，请重新输入！");
                }else{
                    goodInput[exd].setName(name);
                    System.out.println("请选择种类：");
                    String[] kind={goodAllInformation[mark].chooseKinds(goodAllInformation[mark])};
                    goodInput[exd].setKinds(kind);
                    System.out.println("请选择数量：");
                    int count=0;
                    count=goodAllInformation[mark].downCount(goodAllInformation[mark]);
                    goodInput[exd].setCount(count);
                    shopcar-=count;
                    while(shopcar<0){
                        System.out.println("购物车超载，请重新输入该商品数量！");
                        shopcar+=count;
                        count=goodAllInformation[mark].downCount(goodAllInformation[mark]);
                        goodInput[exd].setCount(count);
                        shopcar-=count;
                    }
                }
                exd++;
            }
        }
    }
}
